package com.java2001.service;


import com.java2001.mapper.UserMapper;
import com.java2001.pojo.User;
import com.java2001.utils.DateUtil;
import com.java2001.utils.Encryptor;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public int insertUser(User user){
        String salt = Encryptor.getNewPassword(user.getPassword())[0];
        String password = Encryptor.getNewPassword(user.getPassword())[1];
        user.setPassword(password);
        user.setSalt(salt);
        user.setRegtime(DateUtil.getDate());
        return userMapper.insertUser(user);
    }

    @Override
    public int deleteUser(String username) {
        return userMapper.deleteUser(username);
    }

    @Override
    public int updateUserById(User user) {
        //修改用户密码的时候，需要重新生成盐值和密码
        String salt = Encryptor.getNewPassword(user.getPassword())[0];
        String password = Encryptor.getNewPassword(user.getPassword())[1];
        user.setSalt(salt);
        user.setPassword(password);

        return userMapper.updateUserById(user);
    }

    @Override
    public User queryUserById(String username) {
        return userMapper.queryUserById(username);
    }

    @Override
    public List<User> queryUserList() {
        return userMapper.queryUserList();
    }

}
